<?php

namespace app\api\controller\Product;

use think\Controller;
use think\Request;

class Product extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Product.ProductType');
        $this->ProductModel = model('Product.Product');
        $this->VipModel = model('Product.ProductVip');
    }
    //获取单个类型产品

    public function index()
    {
        $typeid = $this->request->param('typeid', null);

        $flag = $this->request->param('flag', 0);

        $OrderBy = $this->request->param('OrderBy', 'id');

        //搜索查询条件
        $SearchName = $this->request->param('SearchName', '');
        if (!empty($SearchName)) {
            $typeid = $this->TypeModel->where('name', 'like', "%$SearchName%")->value('id');
            // halt($typeid);
        }

        // $UserVip = model('User.User')->where('id',$userid)->value('vip');

        // halt($UserVip);
        if (!empty($typeid)) {
            $where['typeid'] = $typeid;
        }
        // halt($where);
        if ($flag != 0) {
            $where['flag'] = $flag;
        }
        if (!empty($typeid) || $flag != 0) {
            // halt($where);
            $list = $this->ProductModel->where($where)->order($OrderBy, 'desc')->select();
        } else {
            $list = $this->ProductModel->order($OrderBy, 'desc')->select();
        }
        // $list = $this->ProductModel->where($where)->order($OrderBy, 'desc')->select();

        if (!$list) {
            $this->error('暂无数据');
        }

        $this->success('查询成功', null, $list);
    }
    public function type()
    {
        $type = $this->TypeModel->order('id', 'asc')->select();

        if (!$type) {
            $this->error('暂无数据');
        }

        $this->success('查询成功', null, $type);
    }

    public function info()
    {
        $proid = $this->request->param('proid');

        $product = $this->ProductModel->find($proid);

        if (!$product) {
            $this->error('商品不存在');
        }

        $this->success('查询成功', null, $product);
    }
}
